/**
 * Prueba 4
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package coba.service.impl;

import java.util.List;
import java.util.Map;

import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.OrderFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;

import coba.model.Contacto;
import coba.model.Institucion;
import coba.service.base.ContactoLocalServiceBaseImpl;
import coba.service.persistence.ContactoUtil;
import coba.service.persistence.InstitucionUtil;

/**
 * Cambio para probar.
 * The implementation of the Contacto local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link coba.service.ContactoLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author rodri042
 * @see coba.service.base.ContactoLocalServiceBaseImpl
 * @see coba.service.ContactoLocalServiceUtil
 */
public class ContactoLocalServiceImpl extends ContactoLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link coba.service.ContactoLocalServiceUtil} to access the Contacto local service.
	 */
	
	private DynamicQuery createDynamicQueryFromMap(Map<String, String> map, boolean count) {
		DynamicQuery query = DynamicQueryFactoryUtil.forClass(Contacto.class);

		for (Map.Entry<String, String> entry : map.entrySet() ) {
		    String filter = entry.getKey();
		    String value = entry.getValue();
		    
			if (value != null && !value.trim().equals("")) {
				query.add(PropertyFactoryUtil.forName(filter).eq(Long.valueOf(value.trim())));
			} //todos long
		}
		query.addOrder(OrderFactoryUtil.asc("apellido"));

		return query;
	}

	public List<Contacto> findByMap(Map<String, String> map, int start, int end) throws SystemException {
		DynamicQuery query = createDynamicQueryFromMap(map, false);

		return ContactoUtil.findWithDynamicQuery(query, start, end);
	}

	public long countByMap(Map<String, String> map) throws SystemException {
		DynamicQuery query = createDynamicQueryFromMap(map, true);

		return ContactoUtil.countWithDynamicQuery(query);
	}
	
	public List<Contacto> findByInstitucionId(long institucionId) throws SystemException {
		return this.contactoPersistence.findByInstitucionId(institucionId);
	}
}
